package com.czl.module_storehouse.activity.borrow.home;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.czl.base.binding.viewadapter.flycoTabLayout.TabEntity;
import com.czl.base.config.AppConstants;
import com.czl.module_base.activity.BaseTabActivity;
import com.czl.module_service.event.BorrowAddEvent;
import com.czl.module_service.event.BorrowHomeEvent;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.event.BorrowPostEvent;
import com.czl.module_storehouse.event.DetailsPostSuccessEvent;
import com.czl.module_storehouse.fragment.BorrowFragment;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.jeremyliao.liveeventbus.LiveEventBus;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import me.yokeyword.fragmentation.SupportFragment;

/**
 * @author xubin
 */
@Route(path = AppConstants.Router.Service.F_SERVICE_BORROW_HOME)
public class BorrowHomeActivity extends BaseTabActivity {

    private static final int REQUEST_CODE_ADD = 0x11;

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        EventBus.getDefault().register(this);
        mToolBinding.toolbarContentTitle.setText("物品借用");
        LiveEventBus.get(BorrowAddEvent.class).observe(this, event -> new Handler(Looper.myLooper()).postDelayed(() -> {
            binding.viewPager2.setCurrentItem(1);
            LiveEventBus.get(BorrowHomeEvent.class).postOrderly(new BorrowHomeEvent());
        }, 200));
        LiveEventBus.get(DetailsPostSuccessEvent.class).observe(this, event -> new Handler(Looper.myLooper()).postDelayed(() -> {
            binding.viewPager2.setCurrentItem(1);
            LiveEventBus.get(BorrowHomeEvent.class).postOrderly(new BorrowHomeEvent());
        }, 200));
    }

    @Override
    protected List<SupportFragment> fragments() {
        List<SupportFragment> fragmentList = new ArrayList<>();
        fragmentList.add(BorrowFragment.newInstance(0));
        fragmentList.add(BorrowFragment.newInstance(1));
        return fragmentList;
    }

    @Override
    protected ArrayList<CustomTabEntity> mTabEntities() {
        ArrayList<CustomTabEntity> entities = new ArrayList<>();
        entities.add(new TabEntity("待借", R.mipmap.ic_add, R.mipmap.ic_add));
        entities.add(new TabEntity("已借", R.mipmap.ic_add, R.mipmap.ic_add));
        return entities;
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(BorrowPostEvent borrowPostEvent) {
        if (borrowPostEvent != null) {
            binding.viewPager2.setCurrentItem(1);
            BorrowHomeEvent event = new BorrowHomeEvent();
            EventBus.getDefault().postSticky(event);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (REQUEST_CODE_ADD == requestCode) {
            if (RESULT_OK == resultCode) {
                //请求数据
                binding.viewPager2.setCurrentItem(1);
                BorrowHomeEvent event = new BorrowHomeEvent();
                EventBus.getDefault().postSticky(event);
            }
        }
    }
}
